/**
 * Title :	jFamilyTree generator
 * Description: jFamilyTree generator
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.generator.domtree;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Node decorator
 * 
 * @author Bernard Bou
 */
public class DefaultDecorator
{
	// D A T A

	/**
	 * DOM node
	 */
	protected Node theNode;

	// C O N S T R U C T

	/**
	 * Constructor
	 * 
	 * @param thisNode
	 *        DOM node to decorate
	 */
	public DefaultDecorator(Node thisNode)
	{
		theNode = thisNode;
	}

	// S T R I N G S

	// array of names for DOM node-types (indexes = nodeType() values)
	// static private final String[] theTypeName =
	// { "Element", "Attr", "Text", "CDATA", "EntityRef", "Entity", "ProcInstr", "Comment", "Document", "DocType", "DocFragment", "Notation", };

	/**
	 * Get node name
	 * 
	 * @return node name
	 */
	public String getName()
	{
		return theNode.getNodeName();
	}

	/**
	 * Get node data
	 * 
	 * @return node data
	 */
	public String getValue()
	{
		StringBuffer thisBuffer = new StringBuffer();

		// value
		String thisValue = theNode.getNodeValue();
		if (thisValue != null)
		{
			thisBuffer.append(": "); //$NON-NLS-1$

			// trim the value to get rid of newlines at the front
			thisValue = thisValue.trim();
			if ("\n".compareTo(thisValue) == 0) //$NON-NLS-1$
			{
				thisBuffer.append('\u00B6');
			}
			else
			{
				int thisBreak = thisValue.indexOf("\n"); //$NON-NLS-1$
				if (thisBreak >= 0)
					thisValue = thisValue.substring(0, thisBreak);
				thisBuffer.append(thisValue);
			}
		}

		// children
		String thisChildrenText = childrenToString();
		if (thisChildrenText != null)
			thisBuffer.append(thisChildrenText);

		return thisBuffer.toString();
	}

	/**
	 * Convert children's value to string
	 * 
	 * @return string
	 */
	protected String childrenToString()
	{
		StringBuffer thisBuffer = new StringBuffer();
		NodeList thisList = theNode.getChildNodes();
		for (int i = 0; i < thisList.getLength(); i++)
		{
			Node thisNode = thisList.item(i);
			switch (thisNode.getNodeType())
			{
			case Node.TEXT_NODE:
			{
				thisBuffer.append(thisNode.getNodeValue());
				break;
			}
			case Node.ENTITY_REFERENCE_NODE:
			{
				// content is in the TEXT node under it
				thisBuffer.append(new DefaultDecorator(thisNode).toString());
				break;
			}
			case Node.CDATA_SECTION_NODE:
			{
				// "value" has the text, same as a text node.
				// while EntityRef has it in a text node underneath.
				// (because EntityRef can contain multiple subelements)
				// convert angle brackets and ampersands for display
				thisBuffer.append(thisNode.getNodeValue());
				break;
			}
			default:
			{
				// ELEMENT_NODE -- handed separately
				// ATTR_TYPE -- not in the DOM tree
				// ENTITY_TYPE -- does not appear in the DOM
				// PROCINSTR_TYPE -- not "data"
				// COMMENT_TYPE -- not "data"
				// DOCUMENT_TYPE -- Root node only. No data to display.
				// DOCTYPE_TYPE -- Appears under the root only
				// DOCFRAG_TYPE -- equiv. to "document" for fragments
				// NOTATION_TYPE -- nothing but binary data in here
				thisBuffer.append(thisNode.getNodeValue());
				break;
			}
			}
		}
		return thisBuffer.toString();
	}
}